package com.deyuanyun.pic.web.timedtask.tasks;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.tool.StaticParam;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.ThreadUtil;
import com.deyuanyun.pic.common.util.coordinates.CoordinateVO;
import com.deyuanyun.pic.common.util.coordinates.CoordinatesTransfUtils;
import com.deyuanyun.pic.pipe.service.ValveRoomService;
import com.deyuanyun.pic.web.mapper.AllPowerfulMapper;
import com.deyuanyun.pic.web.timedtask.Scheduledable;
import com.deyuanyun.pic.web.timedtask.TaskConfigUtil;
import com.deyuanyun.pic.web.timedtask.TaskPool;
import com.deyuanyun.pic.web.timedtask.TaskTableInfo;
import com.deyuanyun.pic.web.timedtask.tasks.taskservice.CoordinatesConvertService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2016/1/26.
 */
public class CoordinateConvertTask implements Scheduledable {
    static Logger logger = Logger.getLogger(CoordinateConvertTask.class);

    Map map;

    @Override
    public void run() {

        Integer numK= (Integer) map.get("threadNum");
logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+numK);
        boolean b= ThreadUtil.threadIsAliveByName(getScheduledType());
        if (b){
            int iss=ThreadUtil.countThreadNumByName(getScheduledType());
            if (iss>=threadCount()){
                return;
            }
        }

        Thread.currentThread().setName(getScheduledType());

        List<String> taskList= TaskConfigUtil.getTaskList();
        if (ObjectUtil.isEmptyAll(taskList)){
            return;
        }


        CoordinatesConvertService service=SpringAppliction.getBean(CoordinatesConvertService.class);
        if (numK==0){
            service.queryZBFromTable(new HashMap());
        }

        while (!TaskPool.coordTaskTableRecords.isEmpty()){

            List<TaskTableInfo> taskTableInfoList = new ArrayList<>();
            if (TaskPool.coordTaskTableRecords.size() > 99) {
                TaskPool.coordTaskTableRecords.drainTo(taskTableInfoList, 99);
            } else {
                TaskPool.coordTaskTableRecords.drainTo(taskTableInfoList);
            }

            List<CoordinateVO> vos = new ArrayList<>();
            for (TaskTableInfo vo : taskTableInfoList) {
                vos.add(vo.toCoord());
            }

            if (ObjectUtil.isEmptyAll(vos)) {
                break;
            }

            try {
                List<CoordinateVO> lx= CoordinatesTransfUtils.coordConv(vos);
                if (ObjectUtil.isEmptyAll(lx)){
                    continue;
                }
                if (lx.size()==1&&"err".equals(lx.get(0).getErrMark())){
                    AllPowerfulMapper powerfulMapper=SpringAppliction.getBean(AllPowerfulMapper.class);
                    if (powerfulMapper!=null){
                        Map map=new HashMap();
                        String tabName=lx.get(0).getTableName();
                        String id=lx.get(0).getId();
                        if (StringUtils.isNotBlank(tabName)&&StringUtils.isNotBlank(id)){
                            map.put("tableName",tabName);
                            map.put("id",id);
                            powerfulMapper.updateBaiduCoordState(map);
                        }

                    }

                    continue;
                }
                for (CoordinateVO v : lx) {
                    service.updateBaiduCoord(v);
                }
            } catch (Exception e) {
                logger.error("", e);
                continue;
            }
        }
        ThreadUtil.threadRunTime.remove(getScheduledType());
        Thread.currentThread().setName("thread_" + getScheduledType()+ CommonUtil.getRandomStringAndNum(5));
        
        /**
         * 场站阀室 开始计算场站中心点
         */
        ValveRoomService roomService=SpringAppliction.getBean(ValveRoomService.class);
        
        try {
			roomService.updateCenterPoint();
		} catch (Exception e) {
			 logger.error("", e);
			e.printStackTrace();
		}
    }


    @Override
    public String getScheduledType() {
        return StaticParam.CoordinateConvertTask;
    }

    @Override
    public Integer crTimeMinu() {
        return 10;
    }

    @Override
    public Integer threadCount() {
        return 2;
    }

    @Override
    public void setMark(Map x) {
        this.map=x;
    }

    @Override
    public String getMark() {
        return this.getMark();
    }
}
